package com.qidianit.mes.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.Supplier;
import com.qidianit.mes.sys.mapper.SupplierMapper;
import com.qidianit.mes.sys.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @ Description:
 * @ Author:WuJingLei
 * @ Date: 2025/9/16 -20:07
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;
    @Override
    public Result<PageInfo<Supplier>> querySupplier(Supplier supplier,int pageNum,int size) {
        PageHelper.startPage(pageNum,size);
        List<Supplier> suppliers = supplierMapper.querySupplierList(supplier);
        return Result.success(new PageInfo<>(suppliers));
    }

    @Override
    public Result<Supplier> queryByCode(String code) {
        return Result.success(supplierMapper.queryByCode(code));
    }

    @Override
    public Result<Supplier> saveSupplier(Supplier supplier) {
        Supplier supplier1 = supplierMapper.queryByCode(supplier.getCode());
        if (supplier1 != null){
            return Result.error(500,"编码已存在",new Supplier());
        }
        return supplierMapper.addSupplier(supplier)>0?Result.success(supplier):Result.error("操作失败");
    }

    @Override
    public Result updateSupplier(Supplier supplier) {
        Supplier supplier1 = supplierMapper.queryByCode(supplier.getCode());
        if (supplier1 != null && !Objects.equals(supplier1.getId(), supplier.getId())){
            return Result.error(500,"编码已存在",null);
        }
        return supplierMapper.UpdateById(supplier)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result deleteSupplier(Long id) {
        return supplierMapper.delete(id)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result batchDelete(Long[] ids) {
        return supplierMapper.batchDelete(ids)>0?Result.success():Result.error("操作失败");
    }
}
